package org.jeecg.modules.demo.logics.service.impl;

import org.jeecg.modules.demo.logics.entity.TransitInfo;
import org.jeecg.modules.demo.logics.entity.TransitPkg;
import org.jeecg.modules.demo.logics.mapper.TransitPkgMapper;
import org.jeecg.modules.demo.logics.mapper.TransitInfoMapper;
import org.jeecg.modules.demo.logics.service.ITransitInfoService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 运输表
 * @Author: jeecg-boot
 * @Date:   2021-12-16
 * @Version: V1.0
 */
@Service
public class TransitInfoServiceImpl extends ServiceImpl<TransitInfoMapper, TransitInfo> implements ITransitInfoService {

	@Autowired
	private TransitInfoMapper transitInfoMapper;
	@Autowired
	private TransitPkgMapper transitPkgMapper;
	
	@Override
	@Transactional
	public void saveMain(TransitInfo transitInfo, List<TransitPkg> transitPkgList) {
		transitInfoMapper.insert(transitInfo);
		if(transitPkgList!=null && transitPkgList.size()>0) {
			for(TransitPkg entity:transitPkgList) {
				//外键设置
				entity.setTransitId(transitInfo.getId());
				transitPkgMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(TransitInfo transitInfo,List<TransitPkg> transitPkgList) {
		transitInfoMapper.updateById(transitInfo);
		
		//1.先删除子表数据
		transitPkgMapper.deleteByMainId(transitInfo.getId());
		
		//2.子表数据重新插入
		if(transitPkgList!=null && transitPkgList.size()>0) {
			for(TransitPkg entity:transitPkgList) {
				//外键设置
				entity.setTransitId(transitInfo.getId());
				transitPkgMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		transitPkgMapper.deleteByMainId(id);
		transitInfoMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			transitPkgMapper.deleteByMainId(id.toString());
			transitInfoMapper.deleteById(id);
		}
	}
	
}
